/*
 * Write code to remove duplicates from an unsorted linked list
 * FOLLOW UP
 * How would you solve this problem if a temporary buffer is not allow?
 */
package Chapter2;
import java.util.*;


public class Chapter2_1 {

	public void removeDupWithHashTable(MyLinkedList mll)
	{
		ArrayList list = new ArrayList();
		MyLinkedListNode n = mll.Head;
		while(n != null)
		{
			if(list.contains(n.value))
			{
				mll.delete(n.value);
			}
			else{
				list.add(n.value);
			}
			n = n.Next;
		}
	}
	
	public void removeDup(MyLinkedList mll)
	{
		if(mll.Head == null) return;
		MyLinkedListNode current = mll.Head;
		MyLinkedListNode runner;
		while(current != null)
		{
			runner = mll.Head;
			while(current != runner)
			{
				if(current.value == runner.value)
				{
					current = current.Next;
					break;
				}
				runner = runner.Next;
			}
			if(current == runner)
				current = current.Next;
		}	
	}
	
//	public static void main(String args[])
//	{
//		MyLinkedList mll = new MyLinkedList();
//		mll.add(1);
//		mll.add(2);
//		mll.add(3);
//		mll.add(1);
//		mll.add(1);
//		mll.add(4);
//		mll.add(2);
//		Chapter2_1 test = new Chapter2_1();
//		test.removeDupWithHashTable(mll);
//		mll.checkResult();
//	}
}
